package com.admin.web.controller;

import com.admin.web.model.AjaxJson;
import com.bayss.core.constant.ResultMsg;
import com.bayss.core.entity.address.AddressFixedInfo;
import com.bayss.core.service.address.AddressFixedInfoService;
import com.bayss.core.service.address.AddressService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 固定地址管理
 */
@RestController
public class AddressFixedController  extends ApiBaseController{
    @Autowired
    private AddressFixedInfoService addressFixedInfoService;
    @Autowired
    private AddressService addressService;
    /**
     * 固定地址删除
     * @param id
     * @return
     */
    @GetMapping(value = "/fixedAddress/delete.json")
    public ResultMsg deleteById(Long id) {
        try {
            addressFixedInfoService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(-1,"失败");
        }
        return ResultMsg.success();
    }

    /**
     * 固定地址新增
     * @param addressFixedInfo
     * @return
     */
    @PostMapping(value = "/fixedAddress/add.json")
    public ResultMsg add(@RequestBody AddressFixedInfo addressFixedInfo) {
        return addressFixedInfoService.addAddressFixedInfo(addressFixedInfo);
    }

    /**
     * 固定地址修改
     * @param addressFixedInfo
     * @return
     */
    @PostMapping(value = "/fixedAddress/update.json")
    public ResultMsg update(@RequestBody AddressFixedInfo addressFixedInfo) {
        addressFixedInfoService.updateAddressFixedInfo(addressFixedInfo);
        return ResultMsg.success();
    }

    /**
     * 分页查询固定地址
     * @param page
     * @param pageSize
     * @param stationId
     * @param addrAlias
     * @param receivingAddr
     * @return
     */
    @GetMapping(value = "/fixedAddress/getByPage.json")
    public ResultMsg getByPage(Integer page, Integer pageSize, Long stationId, String addrAlias, String receivingAddr) {
        try {
            PageInfo<AddressFixedInfo> pageInfo = addressFixedInfoService.getAddressFixedByPage(page, pageSize, stationId, addrAlias, receivingAddr);
            return ResultMsg.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultMsg.fail(250,"提交失败");
        }
    }

    /**
     * 地址信息
     */
    @RequestMapping("/address/userAddressList.json")
    public ResultMsg userAddressList(Integer uid) {
        return ResultMsg.success(addressService.getByUserId(uid));
    }
}
